package com.controller;

import com.Tool.HCNetSDK;
import com.domain.DoorCar;
import com.domain.DoorDevice;
import com.server.CarServer;
import com.server.DeviceServer;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.*;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/park/interface")
public class testController {

    @Resource
    public DeviceServer deviceServer;
    @Resource
    public CarServer carServer;

    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;

    //保存日志功能每5分钟推送一次信息
    @GetMapping(value = "/zsq")
    public void SetCardInfo(String name, String carId,String starttime,String stoptime,String names,DoorCar carInfoData,byte CardValid) throws UnsupportedEncodingException {
        
        List<DoorDevice> list = new ArrayList<DoorDevice>();
        List list1=new ArrayList();
        String[] car = name.split("=");
        for (int t = 0; t < car.length; t++) {
            list1.add(car[t]);
        }
        String ip=(String) list1.get(0);
        String[] ss = ip.split(",");
        for (int q = 0; q < ss.length; q++) {
            DoorDevice doorDevice = deviceServer.selectName(ss[q]);
            list.add(doorDevice);
        }
        for (int w = 0; w < list.size(); w++) {

            String starTime= (String) list1.get(2);
            String[] time = starTime.split("-");

            String stopTime= (String) list1.get(3);
            String[] stime = stopTime.split("-");
            int iErr = 0;

            //设置卡参数
            HCNetSDK.NET_DVR_CARD_CFG_COND m_struCardInputParamSet = new HCNetSDK.NET_DVR_CARD_CFG_COND();
            m_struCardInputParamSet.read();
            m_struCardInputParamSet.dwSize = m_struCardInputParamSet.size();
            m_struCardInputParamSet.dwCardNum = 1;
            m_struCardInputParamSet.byCheckCardNo = 1;

            Pointer lpInBuffer = m_struCardInputParamSet.getPointer();
            m_struCardInputParamSet.write();

            Pointer pUserData = null;
            FRemoteCfgCallBackCardSet fRemoteCfgCallBackCardSet = new FRemoteCfgCallBackCardSet();
            NativeLong IUserID= lianjie.lianjie(list.get(w).getIp(),list.get(w).getPort(),list.get(w).getAccount(),list.get(w).getPassword());
            NativeLong lHandle = hCNetSDK.NET_DVR_StartRemoteConfig(IUserID, HCNetSDK.NET_DVR_SET_CARD_CFG_V50, lpInBuffer, m_struCardInputParamSet.size(), fRemoteCfgCallBackCardSet, pUserData);
            if (lHandle.intValue() < 0) {
                iErr = hCNetSDK.NET_DVR_GetLastError();
                JOptionPane.showMessageDialog(null, "建立长连接失败，错误号：" + iErr);
                return;
            }
            //JOptionPane.showMessageDialog(null, "建立设置卡参数长连接成功!");
            System.out.println("建立设置卡参数长连接成功!");

            HCNetSDK.NET_DVR_CARD_CFG_V50 struCardInfo = new HCNetSDK.NET_DVR_CARD_CFG_V50(); //卡参数
            struCardInfo.read();
            struCardInfo.dwSize = struCardInfo.size();
            struCardInfo.dwModifyParamType = 0x00000001 + 0x00000002 + 0x00000004 + 0x00000008 +
                    0x00000010 + 0x00000020 + 0x00000080 + 0x00000100 + 0x00000200 + 0x00000400 + 0x00000800;
            /***
             * #define CARD_PARAM_CARD_VALID       0x00000001  //卡是否有效参数
             * #define CARD_PARAM_VALID            0x00000002  //有效期参数
             * #define CARD_PARAM_CARD_TYPE        0x00000004  //卡类型参数
             * #define CARD_PARAM_DOOR_RIGHT       0x00000008  //门权限参数
             * #define CARD_PARAM_LEADER_CARD      0x00000010  //首卡参数
             * #define CARD_PARAM_SWIPE_NUM        0x00000020  //最大刷卡次数参数
             * #define CARD_PARAM_GROUP            0x00000040  //所属群组参数
             * #define CARD_PARAM_PASSWORD         0x00000080  //卡密码参数
             * #define CARD_PARAM_RIGHT_PLAN       0x00000100  //卡权限计划参数
             * #define CARD_PARAM_SWIPED_NUM       0x00000200  //已刷卡次数
             * #define CARD_PARAM_EMPLOYEE_NO      0x00000400  //工号
             * #define CARD_PARAM_NAME             0x00000800  //姓名
             */
            String carNo=(String) list1.get(1);
            String strCardNo = carNo;
            for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++) {
                struCardInfo.byCardNo[i] = 0;
            }
            for (int i = 0; i < strCardNo.length(); i++) {
                struCardInfo.byCardNo[i] = strCardNo.getBytes()[i];
            }


            struCardInfo.byCardValid = CardValid;
            struCardInfo.byCardType = 1;
            struCardInfo.byLeaderCard = 0;
            struCardInfo.byDoorRight[0] = 1; //门1有权限
           struCardInfo.byDoorRight[1] = 1; //门1有权限
   /*         struCardInfo.byDoorRight[2] = 3; //门1有权限
            struCardInfo.byDoorRight[3] = 4; //门1有权限*/
            struCardInfo.wCardRightPlan[0].wRightPlan[0] = 1; //门1关联卡参数计划模板1
            struCardInfo.wCardRightPlan[1].wRightPlan[1] = 1; //门1关联卡参数计划模板1
/*            struCardInfo.wCardRightPlan[0].wRightPlan[2] = 1; //门1关联卡参数计划模板1
            struCardInfo.wCardRightPlan[0].wRightPlan[3] = 1; //门1关联卡参数计划模板1*/

            //卡有效期
            struCardInfo.struValid.byEnable = 1;
            int wYear=Integer.parseInt(time[0]);
             short nwYear= (short)wYear;
            int byMonth=Integer.parseInt(time[1]);
            byte nbyMonths=(byte) byMonth;
            String[] day = time[2].split(" ");
            int byDay=Integer.parseInt(day[0]);
            byte nbyDay=(byte) byDay;
            struCardInfo.struValid.struBeginTime.wYear = nwYear;
            struCardInfo.struValid.struBeginTime.byMonth = nbyMonths;
            struCardInfo.struValid.struBeginTime.byDay = nbyDay;
            struCardInfo.struValid.struBeginTime.byHour = 0;
            struCardInfo.struValid.struBeginTime.byMinute = 0;
            struCardInfo.struValid.struBeginTime.bySecond = 0;

            int wYears=Integer.parseInt(stime[0]);
            short newYear= (short)wYears;
            int byMonths=Integer.parseInt(stime[1]);
            byte nebyMonths=(byte) byMonths;
            String[] days = stime[2].split(" ");
            int byDays=Integer.parseInt(days[0]);
            byte nebyDay=(byte) byDays;
            struCardInfo.struValid.struEndTime.wYear = newYear;
            struCardInfo.struValid.struEndTime.byMonth =nebyMonths;
            struCardInfo.struValid.struEndTime.byDay = nebyDay;
            struCardInfo.struValid.struEndTime.byHour = 0;
            struCardInfo.struValid.struEndTime.byMinute = 0;
            struCardInfo.struValid.struEndTime.bySecond = 0;

            struCardInfo.dwMaxSwipeTime = 0; //无次数限制
            // struCardInfo.dwSwipeTime = 0;
            struCardInfo.byCardPassword = "12346".getBytes();
            struCardInfo.dwEmployeeNo = 22;
           String namess=(String)list1.get(4);
            byte[] strCardName = namess.getBytes("GBK");
            for (int i = 0; i < HCNetSDK.NAME_LEN; i++) {
                struCardInfo.byName[i] = 0;
            }
            for (int i = 0; i < strCardName.length; i++) {
                struCardInfo.byName[i] = strCardName[i];
            }

            struCardInfo.write();
            Pointer pSendBufSet = struCardInfo.getPointer();

            if (!hCNetSDK.NET_DVR_SendRemoteConfig(lHandle, 0x3, pSendBufSet, struCardInfo.size())) {
                iErr = hCNetSDK.NET_DVR_GetLastError();
                JOptionPane.showMessageDialog(null, "ENUM_ACS_SEND_DATA失败，错误号：" + iErr);
                return;
            }

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if (!hCNetSDK.NET_DVR_StopRemoteConfig(lHandle)) {
                iErr = hCNetSDK.NET_DVR_GetLastError();
                JOptionPane.showMessageDialog(null, "断开长连接失败，错误号：" + iErr);
                return;
            }

            // JOptionPane.showMessageDialog(null, "断开长连接成功!");
            System.out.println("断开长连接成功!");
        }
        DoorCar carOne= new DoorCar();
        carOne.setName((String) list1.get(4));
        carOne.setDepartment((String) list1.get(1));
        carOne.setStarttime((String) list1.get(2));
        carOne.setStoptime((String) list1.get(3));
        carOne.setModel((String) list1.get(0));
        if( list1.size()==5) {
            carServer.save(carOne);
        }else{
            int id= Integer.parseInt((String)list1.get(5));
            carServer.updateByCar((String) list1.get(0),(String) list1.get(1),(String) list1.get(4),(String) list1.get(2),(String) list1.get(3),id);
        }
    }
}
